package org.cs.computer;

import lombok.Getter;
import org.cs.calculatemachine.CalculateMachine;
import org.cs.controlmachine.ControlMachine;
import org.cs.program.Instruction;

/**
 * 中央处理器 CPU
 *
 * @author fbin
 * @since 2022/4/5 13:29
 */
@Getter
public class CentralProcessingUnit {
    /**
     * 控制器
     */
    private static final ControlMachine CON;

    /**
     * 运算器
     */
    private static final CalculateMachine CAL;

    static {
        CON = new ControlMachine();
        CAL = new CalculateMachine();
    }

    /**
     * 获取当前指令地址
     *
     * @return 指令地址
     */
    public int getCurrentIrAddress() {
        return CON.getCurrentInstructionAddress();
    }

    /**
     * 获取当前指令
     *
     * @param binaryInstruction 二进制指令
     * @return 解析后的指令
     */
    public Instruction getCurrentInstruction(Object binaryInstruction) {
        CON.analysisInstruction(binaryInstruction);
        return CON.getCurrentInstruction();
    }

    public void setToAcc(int num) {
        CAL.setToAcc(num);
    }

    public int getFromAcc() {
        return CAL.getFromAcc();
    }

    public void addToAcc(int num) {
        CAL.add(num);
    }
}
